package com.zhuss.product.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.site.AllSiteBo;
import com.zhuss.bean.bo.product.site.SearchSiteBo;
import com.zhuss.bean.unit.core.site.SiteBean;
import com.zhuss.bean.vo.product.site.DetailSiteVo;
import com.zhuss.bean.vo.product.site.ListSiteVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.SiteDao;
import com.zhuss.product.db.entity.Site;


/**
 * @description: 站点读
 * @author: zhushanshuo
 * @create: Wed Dec 30 08:43:41 CST 2020
 **/
@Component("siteRead")
public class SiteRead {

	@Autowired
	private SiteDao siteDao;


	public Map<String, SiteBean> listMap() {
		List<SiteBean> list = siteDao.listAll();
		Map<String, SiteBean> map = new HashMap<>();
		for (SiteBean c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}

	/**
	 * 站点
	 * @return
	 */
	public List<SiteBean> listSite() {
		List<SiteBean> result = siteDao.listAll();
		return result;
	}

	/**
	 * 站点
	 * @return
	 */
	public Object listSiteSelect() {
		List<SiteBean> siteList = listSite();
		List<ListVo> result = new ArrayList<>();
		transferList(siteList, result);
		return result;
	}


	/**
	 * 站点
	 * @return
	 */
	public List<SiteBean> listSite(List<Long> siteIdList) {
		List<SiteBean> result = siteDao.listById(siteIdList);
		return result;
	}


	/**
	 * 根据名字查询站点
	 * @param
	 * @return
	 */
	public List<Site> list(String name) {
		List<Site> result = siteDao.listByName(name);
		return result;
	}

	public List<Site> listNotId(String name, Long siteId) {
		List<Site> result = new ArrayList<Site>();
		List<Site> list = list(name);
		for (Site c : list) {
			if (!c.getSiteId().equals(siteId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}


	/**
	 * 站点
	 * @return
	 */
	public Object listSiteId() {
		List<SiteBean> siteList = siteDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(siteList, result);
		return result;
	}

	private void transferListId(List<SiteBean> siteList, List<Long> result) {
		for (SiteBean item : siteList) {
			result.add(item.getSiteId());
		}
	}

	private void transferList(List<SiteBean> siteList, List<ListVo> result) {
		for (SiteBean item : siteList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getSiteId());
			vo.setLabel(item.getName());
			result.add(vo);
		}

	}

	/**
	 * 站点列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchSiteBo bo) {
		PageVo<ListSiteVo> result = new PageVo<ListSiteVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(siteDao.list(bo));
        result.setTotal(siteDao.countList(bo));
		return result;
	}

	/**
	 * 站点详情
	 * @param siteId
	 * @return
	 * @throws DzcxException
	 */
	public DetailSiteVo detail(Long siteId) throws DzcxException {
		DetailSiteVo result = siteDao.detail(siteId);
		if (result == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	
	/**
	 * 所有站点
	 * @param bo
	 * @return
	 * @throws DzcxException
	 */
	public List<ListSiteVo> allSite(AllSiteBo bo) throws DzcxException {
		List<ListSiteVo> result = siteDao.allSite(bo);
		return result;
	}



}
